package shujujiegou;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class SocketClient {
	public static void main(String[] args) {
		Socket socket = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			socket = new Socket("127.0.0.1", 8888);
			os = socket.getOutputStream();
			os.write("hello1".getBytes());
			
			is = socket.getInputStream();
			byte[] bs = readStream(is);
			System.out.print(new String(bs, "UTF-8"));
			System.out.println();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{ 
			try {
				is.close();
				os.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
	}
	public static byte[] readStream(InputStream inputStream) throws IOException {
		int count = 0;  
	    while (count == 0) {  
	        count = inputStream.available();  
	    }  
	    byte[] b = new byte[count];  
	    inputStream.read(b);  
	    return b;  
	}
	
	
}
